package com.example.baiduai.control;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.baiduai.dao.impl.GenericDaoImpl;
import com.example.baiduai.dao.impl.NlpDaoImpl;
import com.example.baiduai.entity.Emotion;
import com.example.baiduai.entity.Items;
import com.example.baiduai.entity.JsonGenericBean;
import com.example.baiduai.entity.JsonRootBean;
import com.example.baiduai.service.NlpService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


/**
 * @author wangyizhu
 * @version "1.8.0_131"
 * @email 2641956765@qq.com
 * @desc:
 * @Time 2022/09/19 14:33
 */

public class NlpControl implements Controller {

    public void handle(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1.设置编码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        //2获取前端获取的参数
        String type = request.getParameter("type");
        String content = request.getParameter("content");
        //去掉前端数据空格
        String str=content.replaceAll(" ","");
        String title = request.getParameter("title");
        NlpService nlpService = new NlpService();
        PrintWriter writer = response.getWriter();
        NlpDaoImpl nlpDao=new NlpDaoImpl();
        GenericDaoImpl genericDao=new GenericDaoImpl();
        //System.out.println(type);
        switch (type) {
            case "emotion":
                //数据库查找
                System.out.println("前端"+str);
                Emotion res = nlpDao.SelectEmotion(str);;
                if (res==null){
                    System.out.println("数据库无数据");
                    String emotion = nlpService.emotion(str);
                    writer.write(emotion);
                    //调用sdk
                    //json字符串转实体类 第一种方式
                    JSONObject emotionJson = JSONObject.parseObject(emotion);
                    JsonRootBean e = JSON.toJavaObject(emotionJson, JsonRootBean.class);
                    List<Items> list= e.getItems();
                    int i = nlpDao.insertEmotion(str,list);
                    if(i==1){
                        System.out.println("成功添加一条缓存数据");
                    }
                }else {
                    System.out.println("数据库有数据");
                    //java对象转json字符串
                    String jsonStr = JSON.toJSONString(res);
                    writer.write(jsonStr);
                }
                break;
            case "address":
               JsonGenericBean bean = genericDao.select("地址识别", str);
               if (bean==null){
                   System.out.println("地址识别，无数据");
                   String address = nlpService.address(str);
                   genericDao.insert("地址识别",str,address);
                   writer.write(address);
               }else {
                   System.out.println("地址识别，有数据");
                   writer.write(bean.getJson());
               }
                break;
            case "lexical":
                JsonGenericBean bean1 = genericDao.select("词法分析", str);
                if (bean1==null){
                    System.out.println("词法分析，无数据");
                    String lexical = nlpService.lexical(str);
                    genericDao.insert("词法分析",str,lexical);
                    writer.write(lexical);
                }else {
                    System.out.println("词法分析，有数据");
                    writer.write(bean1.getJson());
                }
                break;
            case "headline":
                JsonGenericBean bean2 = genericDao.select("新闻摘要", str);
                if (bean2==null){
                    System.out.println("新闻摘要，无数据");
                    String headline = nlpService.headline(str);
                    genericDao.insert("新闻摘要",str,headline);
                    writer.write(headline);
                }else {
                    System.out.println("新闻摘要，有数据");
                    writer.write(bean2.getJson());
                }
                break;
            case "Dialogue":
                JsonGenericBean bean3 = genericDao.select("对话情绪识别", str);
                if (bean3==null){
                    System.out.println("对话情绪识别，无数据");
                    String dialogue = nlpService.Dialogue(str);
                    genericDao.insert("对话情绪识别",str,dialogue);
                    writer.write(dialogue);
                }else {
                    System.out.println("对话情绪识别，有数据");
                    writer.write(bean3.getJson());
                }
                break;
            case "dnnlmCn":
                JsonGenericBean bean4 = genericDao.select("DNN语言模型", str);
                if (bean4==null){
                    System.out.println("DNN语言模型，无数据");
                    String s = nlpService.dnnlmCn(str);
                    genericDao.insert("DNN语言模型",str,s);
                    writer.write(s);
                }else {
                    System.out.println("DNN语言模型，有数据");
                    writer.write(bean4.getJson());
                }
                break;
            case "category":
                String category = nlpService.category(title, str);
                writer.write(category);
                break;
            case "commentTag":
                JsonGenericBean bean7 = genericDao.select("评论观点抽取", str);
                if (bean7==null){
                    System.out.println("评论观点抽取，无数据");
                    String s1 = nlpService.commentTag(str);
                    genericDao.insert("评论观点抽取",str,s1);
                    writer.write(s1);
                }else {
                    System.out.println("评论观点抽取，有数据");
                    writer.write(bean7.getJson());
                }
                break;
            case "depParser":
                JsonGenericBean bean8 = genericDao.select("依存句法分析", str);
                if (bean8==null){
                    System.out.println("依存句法分析，无数据");
                    String depParser = nlpService.depParser(str);
                    genericDao.insert("依存句法分析",str,depParser);
                    writer.write(depParser);
                }else {
                    System.out.println("依存句法分析，有数据");
                    writer.write(bean8.getJson());
                }
                break;
            case "wordEmbedding":
                JsonGenericBean bean9 = genericDao.select("词向量表示", str);
                if (bean9==null){
                    System.out.println("词向量，无数据");
                    String s2 = nlpService.wordEmbedding(str);
                    genericDao.insert("词向量表示",str,s2);
                    writer.write(s2);
                }else {
                    System.out.println("词向量，有数据");
                    writer.write(bean9.getJson());
                }
                break;
            case "wordSimEmbedding":
                 JsonGenericBean bean10 = genericDao.select("词义相似度", str);
                 String s3 = nlpService.wordSimEmbedding(str, title);
                writer.write(s3);
                break;
            case "simnet":
                 String simnet = nlpService.simnet(str, title);
                writer.write(simnet);
                break;
            case "keyword":
                JsonGenericBean bean11 = genericDao.select("文章标签", str);
                 String keyword = nlpService.keyword(str, title);
                writer.write(keyword);
                break;
            case "ecnet":
                JsonGenericBean bean12 = genericDao.select("文本纠错", str);
                if (bean12==null){
                    System.out.println("文本纠错，无数据");
                    String ecnet = nlpService.ecnet(str);
                    genericDao.insert("文本纠错",str,ecnet);
                    writer.write(ecnet);
                }else {
                    System.out.println("文本纠错，有数据");
                    writer.write(bean12.getJson());
                }
                break;
            case "lexerCustom":
                JsonGenericBean bean13= genericDao.select("词法分析（定制版）", str);
                if (bean13==null){
                    System.out.println("词法分析（定制版） 无数据");
                    String s4 = nlpService.lexerCustom(content);
                    genericDao.insert("词法分析（定制版）",str,s4);
                    writer.write(s4);
                }else {
                    System.out.println("词法分析（定制版） 有数据");
                    writer.write(bean13.getJson());
                }
                break;
            default:
                break;
        }
//关流
        writer.flush();
        writer.close();

    }


}
